﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccessLayer;
using DataAccessLayer.DAL;
using Common;
using System.Data;
using System.Xml;
using System.Text;

namespace BestBuy.SellerViews
{
    public partial class SellProductCategory : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.Status != (int)UserStatus.Active)
            {
                Response.Redirect("~");
                return;
            }
            if (!IsPostBack)
            {
                LoadPaymentDetails();
                LoadTreeView(user);
                LoadContactInfo(user);
            }
        }
        
        private void LoadTreeView(User user)
        {
            tvCategories.Nodes.Clear();
            List<ProductCategory> list = ManagerDAL.GetAllCategories();
            List<ProductCategory> SellProducts = SellerDAL.GetSellProduct(user.Username);
            TreeNode root = new TreeNode();
            root.Value = "0";
            root.Text = "Category";
            CreateCategory(list, SellProducts, root, null);
            tvCategories.Nodes.Add(root);
        }

        private void CreateCategory(List<ProductCategory> list, List<ProductCategory> SellProducts, TreeNode parent, int? parentID)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].ParentID == parentID)
                {
                    TreeNode treenode = new TreeNode();
                    treenode.Value = list[i].ID.ToString();
                    treenode.Text = list[i].Name;
                    CreateCategory(list, SellProducts, treenode, list[i].ID);
                    parent.ChildNodes.Add(treenode);
                    //parent.ShowCheckBox = false;
                }
                if (parent.ChildNodes.Count == 0)
                {
                    if (CheckSellProduct(SellProducts, parent))
                        parent.Checked = true;
                }
            }
        }

        private bool CheckSellProduct(List<ProductCategory> SellProducts, TreeNode node)
        {
            foreach (ProductCategory pro in SellProducts)
            {
                    if (node.Value == pro.ID.ToString())
                    {
                        return true;
                    }
            }
            return false;

        }

        private String GetInfoSent()
        {
            String info = "";
            //if (chbName.Checked)
            //    info += InfoSendToBuyer.Name + ",";
            //if (chbEmail.Checked)
            //    info += InfoSendToBuyer.Email + ",";
            //if (chbPhoneNum.Checked)
            //    info += InfoSendToBuyer.Phone + ",";
            
            return info;
        }

        private void LoadContactInfo(User user)
        {
            UserSetting usersetting = SellerDAL.GetUserSetting(user.ID);

            //if (usersetting.ContactInfo.Contains(InfoSendToBuyer.Name.ToString()))
            //    chbName.Checked = true;
            //if (usersetting.ContactInfo.Contains(InfoSendToBuyer.Email.ToString()))
            //    chbEmail.Checked = true;
            //if (usersetting.ContactInfo.Contains(InfoSendToBuyer.Phone.ToString()))
            //    chbPhoneNum.Checked = true;
        }

        private void LoadPaymentDetails()
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user == null || user.Status != (int)UserStatus.Active)
            {
                Response.Redirect("~");
                return;
            }
            PaymentDetail pay = UserDAL.GetPaymentDetailByID(user.ID);
            if (pay != null)
            {
                txtFirstName.Text = pay.FirstName;
                txtLastName.Text = pay.LastName;
                txtCardType.Text = pay.CardType.ToString();
                txtCardNumber.Text = pay.CardNumber;
                txtExpirationDate.Text = pay.ExpirationDate.ToString();
                txtCSC.Text = pay.CSC;
                txtBillingAddress.Text = pay.BillingAddress;
                txtBillingAddress2.Text = pay.BillingAddress2;
                txtCity.Text = pay.City;
                txtCountry.Text = pay.Country;
                txtPostalCode.Text = pay.PostalCode;
                txtState.Text = pay.State;
                txtPaypalEmail.Text = pay.PaypalEmail;
            }
        }

        protected void btnUpdateContact_Click(object sender, EventArgs e)
        {
            User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
            if (user != null)
            {
                String info = GetInfoSent();
                SellerDAL.UpdateInfoSentBuyer(user.ID, info);
            }
        }

        protected void btnResetContact_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdatePayment_Click(object sender, EventArgs e)
        {
            try
            {
                User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
                if (user == null || user.Status != (int)UserStatus.Active)
                {
                    Response.Redirect("~/Account/Login.aspx");
                    return;
                }
                PaymentDetail pay = new PaymentDetail();
                int cardtype = 0;
                pay.UserID = user.ID;
                pay.FirstName = txtFirstName.Text;
                pay.LastName = txtLastName.Text;
                Int32.TryParse(txtCardType.Text, out cardtype);
                if (cardtype != 0)
                    pay.CardType = cardtype;
                if (txtExpirationDate.Text != "")
                    pay.ExpirationDate = DateTime.Parse(txtExpirationDate.Text);
                pay.CardNumber = txtCardNumber.Text;
                pay.CSC = txtCSC.Text;
                pay.BillingAddress = txtBillingAddress.Text;
                pay.BillingAddress2 = txtBillingAddress2.Text;
                pay.City = txtCity.Text;
                pay.Country = txtCountry.Text;
                pay.PostalCode = txtPostalCode.Text;
                pay.State = txtState.Text;
                pay.PaypalEmail = txtPaypalEmail.Text;
                if (UserDAL.UpdatePaymentDetail(pay))
                {
                    PaymentDetailsMessage.Show("Update Payment Details success.", UserControls.MessagePanel.MessageType.Success);
                }
                else
                {
                    PaymentDetailsMessage.Show("Update Payment Details error.", UserControls.MessagePanel.MessageType.Error);
                }
            }
            catch { }
        }

        protected void btnResetPayment_Click(object sender, EventArgs e)
        {
            LoadPaymentDetails();
        }

        protected void btnUpdateCategory_Click(object sender, EventArgs e)
        {
            try
            {
                User user = UserDAL.GetUserByUsername(HttpContext.Current.User.Identity.Name);
                if (user == null || user.Status != (int)UserStatus.Active)
                {
                    Response.Redirect("~");
                    return;
                }
                List<ProductCategory> list = new List<ProductCategory>();
                foreach (TreeNode node in tvCategories.CheckedNodes)
                {
                    ProductCategory product = new ProductCategory();
                    product.ID = int.Parse(node.Value);
                    list.Add(product);
                }
                SellerDAL.AddSellProduct(user.ID, list);
            }
            catch  { }
        }

        protected void btnResetCategory_Click(object sender, EventArgs e)
        {

        }

    }
}